package org.zhaozhen.pet.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhaozhen.basic.service.impl.BaseServiceImpl;
import org.zhaozhen.basic.util.CodeGenerateUtils;
import org.zhaozhen.basic.util.DistanceUtil;
import org.zhaozhen.basic.util.PageList;
import org.zhaozhen.basic.util.Point;
import org.zhaozhen.order.domain.PetAcquisitionOrder;
import org.zhaozhen.order.mapper.PetAcquisitionOrderMapper;
import org.zhaozhen.org.domain.Employee;
import org.zhaozhen.org.domain.Shop;
import org.zhaozhen.org.mapper.EmployeeMapper;
import org.zhaozhen.org.mapper.ShopMapper;
import org.zhaozhen.pet.domain.Pet;
import org.zhaozhen.pet.domain.SearchMasterMsg;
import org.zhaozhen.pet.mapper.PetMapper;
import org.zhaozhen.pet.mapper.SearchMasterMsgMapper;
import org.zhaozhen.pet.query.SearchMasterMsgQuery;
import org.zhaozhen.pet.service.IPetService;
import org.zhaozhen.pet.service.ISearchMasterMsgService;
import org.zhaozhen.user.domain.Logininfo;
import org.zhaozhen.user.domain.User;
import org.zhaozhen.user.mapper.LogininfoMapper;
import org.zhaozhen.user.mapper.UserMapper;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Logininfo currentLogin) {
        //获取当前登录人
        User user = userMapper.loadByLogininfo_id(currentLogin.getId());
        //为searchMasterMsg的user_id赋值
        searchMasterMsg.setUser_id(user.getId());
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        searchMasterMsg.setShop_id(nearestShop.getId());
        searchMasterMsgMapper.save(searchMasterMsg);

    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo currentLogin) {
        Employee employee = employeeMapper.loadByLogininfo_id(currentLogin.getId());
        query.setShopId(employee.getShop_id());
        query.setState(0);
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo currentLogin) {
        Employee employee = employeeMapper.loadByLogininfo_id(currentLogin.getId());
        query.setShopId(employee.getShop_id());
        query.setState(1);
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> queryUserMsg(SearchMasterMsgQuery query, Logininfo currentLogin) {
        User user = userMapper.loadByLogininfo_id(currentLogin.getId());
        query.setUserId(user.getId());//查询当前用户的数据
        return super.queryPage(query);

    }

    @Override
    public void handle(Pet pet, Logininfo currentLogin) {
        //将宠物信息状态改为已处理
        searchMasterMsgMapper.loadByIdState(pet.getSearch_master_msg_id());
        //保存宠物信息
        petService.add(pet);
        //生成购买订单
        //先拿到未处理的寻主消息
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        //拿到当前登录人对象
        Employee employee = employeeMapper.loadByLogininfo_id(currentLogin.getId());
        PetAcquisitionOrder order = pet2order(pet, searchMasterMsg, employee.getId());
        petAcquisitionOrderMapper.save(order);
    }


    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
